Home |
| Latest | About | Random
# Week 0 ```cpp /* The story last time... (since Week 0 + reading Sec. 2 and 3) This is a comment block, enclosed by a matching pair of slash-star and star-slash. Code in a comment block is ignored will not be executed. Doesn't matter where! Here is a simple C++ program that will print out some text to the console. Syntax matters! C++ statements need to end in a semicolon ; Lines starting with # are called "Preprocessing Directives", and do not end them with ; is best practice. Function definition type func(...){...} do not need a semicolon after the } */ // This is an inline comment. Anything after slash-slash will // be ignored and not executed. // Finally, extraneous spaces are ignored. #include /* this includes the C++ standard libary header for std::cout, std::endl */ using namespace std; /* this line enables us to write cout instead of std::cout */ int main(){ // cout << "..." to print the string literal "..." to console, // \n is a newline character cout << "Hello, World!\n"; // we can chain togheter << with cout cout << "One, two,..." << " three" << " four!\n"; // endl is another way to go to the next line cout << "What a wonderful World!" << endl; /* We can use the excape character \ to create escape sequences, like \n for new line. To get other characters, for example, \" for a double-quote, and \\ for a slash. */ cout << "Look at this \"Cat\"!" << endl; cout << " /\\\"/\\\n" // extra spacing is irrelevant << " \\o o/\n" // use them to help format code << " =_^_=\n"; return 0; // the following is not executed as we've already reached return. cout << "I am left out!"; } /* A typical C++ program must have one and exactly one main(){...} function. Inside the curly braces {...} contains instructions to be executed. These instructions consist of valid C++ statements, that ends in semicolon ; executed in sequence. The main() function is the entry point of the C++ program, which is where it starts. It is required of a C++ program to return an integer back to the operating system to indicate its status (exit status code), and it is customary to have exit code of 0 after normal execution. We do this by writing the statement: return 0; The program also ends if it reaches return in the main(){...} function. Since main() is a C++ function (we will disucss it more later), declaring a C++ function we must also declare its return type, that is, what is the type of the "value" it gives back. Since we are going to return 0 in our main function, 0 is an integer, which has type int. This is why we write int main(){....} Do not sweat this for now, but just for a taste. The mathematical function f: Z -> Z where f(x) = x + 3 is a function whose name is f, that takes in an integer x, and returns an integer x + 3. In C++ this would be written as int f(int x){ return x + 3; } To print something to the console, we need to "include" this functionality. There is a C++ standard library to do this, which is , for input-output stream. To invoke these functionalities, we include this standard library header file by writing #include Now, we can use the standard library function std::cout to print out a string literal to the console. A string literal is a sequence of characters enclused in a pair of matching double quotes "....". Later we will see std::cout can also print out other things besides string literals. To print out a string literal "..." using std:cout, we use the binary operator << , like this std::cout << "..."; For now, you can just imagine the string literal "..." gets "eaten", or "flows" into std::cout, which tells it what to print. We can also chain together string literals to "flow into" std::cout, so long as they are all on the right: std::cout << "one..." << "two..." << "three..."; There are two ways to print to the next line, one is to use the escape sequence \n called the newline character in your string literal.This newline character \n is one single character. Another way for newline is to "flow into" std::cout the standard function std::endl, which is also from std::cout << "This is one line" << std::endl << "On next line"; Finally, note we define a string literal by enclosing characters in double-quotes "...". But what if we want to print out a double-quote character? It'd be confusing to have "..."...". This is where we use the escape character \ and use the escape sequence \" for double-quote. This escape sequence \" is one single character representing a double-quote. And what if we want our string literal to include a blackslash \ character? To prevent \ to be treated as an escape character, we use the escape sequence \\ to represent the \ character. Some challenges: 1) *Pushes glasses* WELL ACKTUALLY.... the program int main(){ return 0; } is actually not the shortest C++ program that will compile and run, it can be made even shorter. Certainly unnecessary spaces can be removed, but we can actually remove a bit more. Now, I still will NOT recommend you write it this short way, eventhough the standard allows for it. Try to see if you can find out which bit can be further removed, and look it up. AGAIN, GOOD PRACTICE TO NOT TO DO WHAT YOU FOUND HERE! 1) See if you can print out this nice banner to the console 8888888888888888888888888888888888888888888888888888888 8888888P88888888888888888888888888888888888888888888888 88888 "888888" "88888888888888 888888888888888888 8888 9o "8888P 8888888888888 88 888888888888888888 8888888" 98888 d888888888888 8P 8888888888888888888 8888888 8888P 888" 98888 P 8888 '9P"988888888 888888 98888 888 d8b 9888 d88 d8o .88888888 88888b 88888 88 d88 888 d88 8888 888888888 88888b .8888 8P 888b888 988 888P 888888888 88888b "8 "8888P d8b P 988" 8P 888888 888888b. ob db. .d8888. 88 d8 d888888 8888888888888888888888888888888888888888888888888888888 8888888888888888888888888888888888888888888888888888888 Be on the look out for certain characters that needs escaping! */ ```